package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.MyCircleMembersListAdapter;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseConfirmDialogFragment;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.VolleyResponse;
import com.isunland.managesystem.entity.MyCircle;
import com.isunland.managesystem.entity.MyCircleMembers;
import com.isunland.managesystem.entity.MyCircleMembersOriginal;
import com.isunland.managesystem.entity.SuccessMessage;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.MyUtils;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONException;

/**
 * 对象功能:   圈子成员列表
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/3/16 19:24
 */
public class MyCircleMembersListFragment extends BaseListFragment {
  protected static final String EXTRA_VALUE =
      "com.isunland.managesystem.ui.MyCircleMembersListFragment.EXTRA_VALUE";
  private final int REQUEST_ADD = 0;//添加好友
  private final int REQUEST_OUT = 1;//退出此圈子
  private MyCircle mcontent;
  private ArrayList<MyCircleMembers> mList;

  @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    setBack(true);
    mcontent = (MyCircle) getArguments().getSerializable(EXTRA_VALUE);
    mList = new ArrayList<>();
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_add_out, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        getActivity().finish();
        return true;
      case R.id.menu_item_add:
        //添加好友
        Intent add = new Intent(getActivity(), MyCircleMembersAddActivity.class);
        add.putExtra(MyCircleMembersAddFragment.EXTRA_CIRCLEID, mcontent.getId());//圈子id
        startActivityForResult(add, REQUEST_ADD);
        return true;
      case R.id.menu_item_out:
        //退出此圈子
        DialogFragment dialogFragment = BaseConfirmDialogFragment.newInstance("是否确认退出此圈子?");
        FragmentManager fm = getFragmentManager();
        dialogFragment.setTargetFragment(this, REQUEST_OUT);
        dialogFragment.show(fm, "");
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override public String getUrl() {
    return ApiConst.URL_MY_CIRCLE_MEMBER;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("mainid", mcontent.getId());
    return params;
  }

  @Override public void resolveResponse(String json) {
    MyCircleMembersOriginal original = new Gson().fromJson(json, MyCircleMembersOriginal.class);
    if (original.getResult() == Base.SUCCESS && original.getRows() != null) {
      //获取列表成功
      mList.clear();
      mList.addAll(original.getRows());
      MyCircleMembersListAdapter mAdapter = new MyCircleMembersListAdapter(getActivity(), mList);
      setListAdapter(mAdapter);
    } else {
      ToastUtil.show("获取列表失败");
    }
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == REQUEST_ADD) {
      volleyPost();
    }
    if (requestCode == REQUEST_OUT) {

      volleyPostOutCircle();
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  private void volleyPostOutCircle() {
    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_MY_CIRCLE_OUT);
    HashMap<String, String> params = new HashMap<>();
    params.put("id", mcontent.getId());
    MyUtils.showDialogLoading(getActivity(), "退出圈子");
    mActivity.volleyPost(url, params, new VolleyResponse() {
      @Override public void onVolleyError(VolleyError arg0) {

      }

      @Override public void onVolleyResponse(String arg0) throws JSONException {
        MyUtils.dismissDialog();
        try {
          SuccessMessage successsMessage = new Gson().fromJson(arg0, SuccessMessage.class);

          String successType = successsMessage.getResult();

          if (successType.equals("1")) {
            ToastUtil.show(R.string.success_operation);
            getActivity().setResult(Activity.RESULT_OK);
            getActivity().finish();
          } else {
            ToastUtil.show(R.string.failure_operation);
          }
        } catch (Exception e) {
          LogUtil.e("error", e);
          ToastUtil.show(R.string.failure_operation);
        }
      }
    });
  }

  public static MyCircleMembersListFragment newInstance(MyCircle content) {

    Bundle args = new Bundle();
    args.putSerializable(EXTRA_VALUE, content);
    MyCircleMembersListFragment fragment = new MyCircleMembersListFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
